# vim: ts=4:sw=4:syntax=perl
# CSS-V1

use gri_secmgr qw(get_cookie_value get_user_name);
use gri_userprefs;
# One argument passed - the way to sort the returned list 

sub mm_portal_report_list($$) {
my $order_by=lc($_[0]);
my $portal=$_[1];

	my $v=get_user_name();
	my $users_dir=$::GRI_FRONTEND->get_global_directory("users");
	my $show_hovers=0;
	if(-f "$users_dir/$v-prefs.xml") {
		my $xx=new gri_userprefs("$users_dir/$v-prefs.xml");
		if($xx) {
			my ($d,$d2);
			($d,$d2)=$xx->get_pref_value("hover_report_info");
			$show_hovers=$d2 if(defined($d2));
		}
	}
	my ($rep_admin,$err2)=(0,undef);
	if($v && $v ne "public") {
		($rep_admin,$err2)=$::GRI_FRONTEND->does_user_have_role($v,"report_admin");
		$rep_admin=0 if(!defined($rep_admin));
	}
	$show_hovers=to_bool($show_hovers);
	my $p2=hex_to_string($portal);
	my $p3=$::GRI_FRONTEND->get_portal_filename($p2);

	if(! $::GRI_FRONTEND->is_user_allowed($p2,$v)) {
		return "Security Error: User '$v' not allowed access to portal '$p2' [file='$p3'].";
	}
	my ($lref,$err)=$::GRI_FRONTEND->get_portal_report_names($p2);
	if(!defined($lref)) {
		return "Error: mm_portal_report_list :: $err";
	}
	my @L=(@$lref);
	if($order_by eq "?order_by - not defined?") {
		$order_by="name";
	}
	# Name is unique - so only sort by it.	#
	if($order_by eq "rname") {
		@L=sort {$::GRI_FRONTEND->get_report_name($b) cmp $::GRI_FRONTEND->get_report_name($a);} @L;
	} elsif($order_by eq "name") {
		@L=sort {$::GRI_FRONTEND->get_report_name($a) cmp $::GRI_FRONTEND->get_report_name($b);} @L;
	} elsif($order_by eq "type") {
		# Sort by type and then by name
		@L=sort {my $a1=$::GRI_FRONTEND->get_report_type($a);
			 my $b1=$::GRI_FRONTEND->get_report_type($b);
			 if($a1 ne $b1) {return $a1 cmp $b1;}
			 $::GRI_FRONTEND->get_report_name($a) cmp $::GRI_FRONTEND->get_report_name($b);
			 } @L;
	} elsif($order_by eq "rtype") {
		# Sort by rtype and then by name
		@L=sort {my $a1=$::GRI_FRONTEND->get_report_type($a);
			 my $b1=$::GRI_FRONTEND->get_report_type($b);
			 if($a1 ne $b1) {return $b1 cmp $a1;}
			 $::GRI_FRONTEND->get_report_name($a) cmp $::GRI_FRONTEND->get_report_name($b);
			 } @L;
	} elsif($order_by eq "treports") {
		# If two have the same number sort by name
		@L=sort {
			my @a1=$::GRI_FRONTEND->get_portal_report_details($p2,$a);
			my @b1=$::GRI_FRONTEND->get_portal_report_details($p2,$b);
			if($a1[5] != $b1[5]) {return $a1[5] <=> $b1[5]; }
			$::GRI_FRONTEND->get_report_name($a) cmp $::GRI_FRONTEND->get_report_name($b);
			} @L;
	} elsif($order_by eq "rtreports") {
		# If two have the same number sort by name
		@L=sort {
			my @a1=$::GRI_FRONTEND->get_portal_report_details($p2,$a);
			my @b1=$::GRI_FRONTEND->get_portal_report_details($p2,$b);
			if($a1[5] != $b1[5]) {return $b1[5] <=> $a1[5]; }
			$::GRI_FRONTEND->get_report_name($a) cmp $::GRI_FRONTEND->get_report_name($b);
			} @L;
	} elsif($order_by eq "report") {
		# If two have the date number sort by name
		@L=sort {
			my @a1=$::GRI_FRONTEND->get_portal_report_details($p2,$a);
			my @b1=$::GRI_FRONTEND->get_portal_report_details($p2,$b);
			if($b1[2] ne $a1[2]) {return $b1[2] cmp $a1[2]; }
			$::GRI_FRONTEND->get_report_name($a) cmp $::GRI_FRONTEND->get_report_name($b);
			} @L;
	} elsif($order_by eq "rreport") {
		# If two have the date number sort by name
		@L=sort {
			my @a1=$::GRI_FRONTEND->get_portal_report_details($p2,$a);
			my @b1=$::GRI_FRONTEND->get_portal_report_details($p2,$b);
			if($b1[2] ne $a1[2]) {return $a1[2] cmp $b1[2]; }
			$::GRI_FRONTEND->get_report_name($a) cmp $::GRI_FRONTEND->get_report_name($b);
			} @L;
	}

	# We simply show the list of reports in a table.

	my $s="";
	my $_n1=string_to_hex("1");
	my $row=0;
	my $alt="";
	for my $c (@L) {
		next if !defined($c);
		my $n2=string_to_hex($c);
		my @info=$::GRI_FRONTEND->get_portal_report_details($p2,$c);

		if(defined($show_hovers) && $show_hovers && defined($info[1])) {
			$alt="title=\"host: " . _html($info[1]) . "\nDirectory: " . _html($info[10]) . "\nPattern: " . _html($info[11]) . "\"";
		}
		if(!defined($info[0])) {
			$info[0]="No information availalble for report '$c'.";
			if(defined($info[5])) {
				$info[5] =~ s/^ERROR:\s+//;
				$info[0]="Error getting info for report '$c': $info[5]";
			}
			$info[5]="";
			$info[2]="";
		}
		$info[4]="" if !defined($info[4]) || $info[4] eq "";

		my $l3=undef;
		my $l2=undef;
		my $l4=undef;
		if($rep_admin) {
			my $page=string_to_hex($::ENV{REQUEST_URI});
			$l4=link_to_str("stdlink","Edit Report Definition","form_modify_report","rep_name=$n2,return_to=$page");
		}
		if(defined($info[6]) && length($info[6])) {
			if($attrs !~ /converted_only/) {
				$l2=link_to_str("stdlink","View Latest Report ($info[4])","portal_file_view","portal=$portal,repname=$n2,repnum=$_n1,convert=0");
				$l3=link_to_str("stdlink","View Latest Report ($info[6])","portal_file_view","portal=$portal,repname=$n2,repnum=$_n1,convert=1");
				$info[4].=" / $info[6]";
			} else {
				$l3=link_to_str("stdlink","View Latest Report","portal_file_view","portal=$portal,repname=$n2,repnum=$_n1,convert=1");
				$info[4]=$info[6];
			}
		}
		my @llist=();
		my $n=string_to_hex($c);
		if(defined($info[0]) && (length($info[5]) && $info[5]>0)) {
			my $l=link_to_str("stdlink","View Report List","portal_report_list","portal=$portal,repname=$n2");
			if(! (defined($info[6]) && length($info[6]))) {
				$l2=link_to_str("stdlink","View Latest Report","portal_file_view","portal=$portal,repname=$n2,repnum=$_n1,convert=0");
			}
			push @llist,$l;
			push @llist,$l2 if(defined($l2));
			push @llist,$l3 if(defined($l3));
		}
		my $class="gri_odd";
		$class="gri_even" if $row%2==0;
		if($info[2] eq "" && $info[5] eq "") {
			  $s.="<tr id=$class>
			    <td style=gri_cell-error colspan=5><font style=\"font-weight: bold; text-align: center;\">&nbsp;$info[0]<br>&nbsp;</font></td></tr>\n";	} else {
			$s.="
  <tr id=$class>
    <td class=gri_cell $alt>$info[0]</td>
    <td class=gri_cell-center>$info[4]</td>\n";
	my $tstr="";
	if($info[2] ne '0') {
		my $tref=$::GRI_FRONTEND->internal_to_posix_time($info[2]);
		$tstr=$::GRI_FRONTEND->date_str($tref);
	}

	push @llist,$l4 if(defined($l4));
    $s.="<td class=gri_cell-center>$tstr</td>
    <td class=gri_cell-center>$info[5]</td>";
		$s.="    <td class=gri_cell-right>" . join("<br>",@llist) . "</td></tr>";
		}
		$row++;
	}
	$s.="</table>";
	return $s;
}

